<?php
class Request extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->check_isvalidated();
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	}

	public function index()
	{
		$data = get_initial_data($this);
		$data['title'] = 'Request';
		$this->load->model('request_model');
		$data['requests'] = $this->request_model->get_new_request();
		$data['receives'] = $this->request_model->get_item_not_received_request();
		$this->load->view('templates/header', $data);
		$this->load->view('request/main', $data);
		$this->load->view('templates/footer', $data);
	}

	public function single_approve(){
		$request_id =$this->input->post('request_id');
		if ($request_id){
			$this->load->model('request_model');
			$this->load->model('inventory_model');
			$this->load->model('price_rule_model');
			$this->load->model('promotion_model');
			$request = $this->request_model->get_request_by_id($request_id);
			if ($request->request_approved_date == null){
				$inventory = $this->inventory_model->get_inventory_by_item_id($request->item_id);
				if ($inventory->inventory_current_stock < $request->request_amount){
					array_push($error, array($request, Messages::not_enough_stock));
				}else {
					if ($this->inventory_model->reduce_stock($request->item_id, $request->request_amount)){
						$percent= ($inventory->inventory_current_stock-$inventory->inventory_min_stock-$request->request_amount)/$inventory->inventory_min_stock*100;
						$discount = $this->price_rule_model->get_discount($percent);
						$this->promotion_model->update_discount($request->item_id, $discount);
						if ($this->request_model->approved($request->request_id)){
							echo 1;
							return;
						}
					}
				}
			}
			echo 0;
		}else{
			redirect('');
		}
	}

	public function approve()
	{
		$data = get_initial_data($this);
		$error = array();
		$requests_id = $this->input->post('chk');
		$this->load->model('request_model');
		$this->load->model('inventory_model');
		$this->load->model('price_rule_model');
		$this->load->model('promotion_model');
		$count = 0;
		if ($requests_id != null){
			foreach ($requests_id as $request_id){
				$request = $this->request_model->get_request_by_id($request_id);
				if ($request->request_approved_date == null){
					$inventory = $this->inventory_model->get_inventory_by_item_id($request->item_id);
					if ($inventory->inventory_current_stock < $request->request_amount){
						$request->reason = Messages::not_enough_stock;
						array_push($error, $request);
					}else {
						if ($this->inventory_model->reduce_stock($request->item_id, $request->request_amount)){
							echo 'herer';
							$percent= ($inventory->inventory_current_stock-$inventory->inventory_min_stock-$request->request_amount)/$inventory->inventory_min_stock*100;
							$discount = $this->price_rule_model->get_discount($percent);
							$this->promotion_model->update_discount($request->item_id, $discount);
							$this->request_model->approved($request->request_id);
							$count ++;
						}else{
							$request->reason = Messages::invalid_item;
							array_push($error, $request);
						}
					}
				}else{
					$request->reason = Messages::request_has_been_approved;
					array_push($error, $request);
				}
			}
		}

		$data['title'] = 'Request';
		$data['count'] = $count;
		$data['error'] = $error;
		$this->load->view('templates/header', $data);
		$this->load->view('request/index', $data);
		$this->load->view('templates/footer', $data);
	}

	public function search(){
		$data = get_initial_data($this);
		$this->form_validation->set_rules('search', 'search',  'trim|required|xss_clean');
		if ($this->form_validation->run()){
			$this->load->model('manufacturer_model');
			$string = $this->input->post('search');
			$data['result'] = $this->manufacturer_model->search($string);
		}else{
			$data['result'] = null;
		}
		$data['title'] = 'item';
		$this->load->view('templates/header', $data);
		$this->load->view('manufacturer/search', $data);
		$this->load->view('templates/footer', $data);
	}

	private function check_isvalidated(){
		if(!$this->session->userdata('validated')){
			redirect('');
		}
	}
}